<template>
  <div :id="props.id" ref="echartsDom" :style="props.styleName" :class="props.className"></div>
</template>

<script setup lang="ts">
import * as echarts from 'echarts';
import { Ref, ref, onMounted, nextTick, defineProps, watch } from 'vue';

const echartsDom: Ref<HTMLElement | any> = ref(null);
const charts = ref<echarts.ECharts>();
const props = defineProps({
  id: {
    type: String,
  },
  styleName: {
    type: Object,
  },
  className: {
    type: String,
  },
  option: {
    type: Object,
  },
});
onMounted(() => {
  nextTick(() => {
    if (charts.value == null) {
      charts.value = echarts.init(echartsDom.value);
    }

    if (props.option) {
      charts.value.setOption(props.option);
    }
  });
});
watch(
  () => props.option,
  (newVal, oldVal) => {
    charts.value.setOption(props.option);
  },
);
</script>

<style></style>
